package com.wx.commons;

import java.util.EnumMap;
import java.util.Map;

/**
 * 上下文
 * @author dingyaming
 *
 */
public class EnumContext<K extends Enum<K>> implements Context<K> {

	private Map<K, Object> map;
	
	public EnumContext(Class<K> clazz) {
		super();
		this.map = new EnumMap<K, Object>(clazz);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T get(K key,Class<T> clazz){
		Object object = map.get(key);
		if(object == null){
			return null;
		}
		if(object.getClass().isAssignableFrom(clazz)){
				return (T) object;
		}
		return null;
	}
	
	@Override
	public Object getObject(K key) {
		return get(key, Object.class);
	}
	
	@Override
	public String getString(K key){
		return get(key, String.class);
	}
	
	@Override
	public Integer getInteger(K key){
		return get(key, Integer.class);
	}

	@Override
	public void put(K key, Object value) {
		map.put(key, value);
	}

	@Override
	public void clear() {
		map.clear();
	}


	@Override
	public Boolean getBoolean(K key) {
		return get(key, Boolean.class);
	}


}
